package Top_Interview_Questions.Sort;


import java.util.*;


/**
 * @Author: 吕庆龙
 * @Date: 2020/1/27 20:39
 * <p>
 * 功能描述:
 */
public class _0179 {
    String[] asStrs = new String[5];

    public static void main(String[] args) {
        _0179 test = new _0179();
        int[] nums = {3, 30, 34, 5, 9};
        //9534330
        String number = test.largestNumber(nums);
        System.out.println(number);
    }

    /**
     * https://leetcode-cn.com/problems/largest-number/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-4-6/
     */
    public String largestNumber(int[] nums) {
        Integer[] n = new Integer[nums.length];
        for (int i = 0; i < nums.length; i++) {
            n[i] = nums[i];
        }
        Arrays.sort(n, new Comparator<Integer>() {
            @Override
            public int compare(Integer n1, Integer n2) {
                String s1 = n1 + "" + n2;
                String s2 = n2 + "" + n1;
                //compareTo 方法
                //如果参数是一个按字典顺序排列等于该字符串的字符串，则返回值为0;
                //如果参数是按字典顺序大于此字符串的字符串，则返回值小于0;
                //如果参数是按字典顺序小于此字符串的字符串，则返回值大于0。
                return s2.compareTo(s1);
            }
        });
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < nums.length; i++) {
            sb.append(n[i]);
        }
        String res = sb.toString();
        return res.charAt(0) == '0' ? "0" : res;
    }


}
